package gui.visu.utils;

import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author Charles Perin
 * 29 mars 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 *
 */
public class MagnetList {

	private CopyOnWriteArrayList<Integer> values;

	public MagnetList(){
		setValues(new CopyOnWriteArrayList<Integer>());
	}

	public void setValues(CopyOnWriteArrayList<Integer> values) {
		this.values = values;
	}

	public CopyOnWriteArrayList<Integer> getValues() {
		return values;
	}

	public int magnet(int xMouse, int magnetRatio){
		int closest=this.getValues().get(0);
		for(int i:this.getValues()){
			if(Math.abs(xMouse-i)<=magnetRatio && Math.abs(xMouse-i)<Math.abs(xMouse-closest)){
				closest=i;
			}
		}
		if(Math.abs(xMouse-closest)<=magnetRatio)return closest;
		else return xMouse;
	}
}
